//
//  HomeTrueTradeTraceViewController.m
//  1gene
//
//  Created by 1jiyin on 16/5/24.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "HomeTrueTradeTraceViewController.h"
#import "LogisticsDetailViewController.h"
#import "HealthReportCommentViewController.h"
#import "UMSocial.h"


@interface HomeTrueTradeTraceViewController ()<UMSocialUIDelegate>

@property(nonatomic,copy)NSMutableArray *dataArray;

@property(nonatomic,copy)NSDictionary *dataDic;

@property(nonatomic,copy)UIScrollView *myScrollView;

@end

@implementation HomeTrueTradeTraceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:[UIView new]];
    [self createNavigation];
    self.view.backgroundColor=[UIColor whiteColor];
    //[self createUI];
    // Do any additional setup after loading the view.
}
-(void)createUI
{
    if (_myScrollView) {
        [_myScrollView removeFromSuperview];
    }
    UIScrollView *scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
    scroll.showsVerticalScrollIndicator=NO;
    [self.view addSubview:scroll];
    _myScrollView=scroll;
    
    UIView *subView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, scroll.frame.size.width, scroll.frame.size.height)];
    subView.backgroundColor=[UIColor whiteColor];
    [scroll addSubview:subView];
    
    NSArray *arr=[NSArray arrayWithArray:_dataArray];
    //NSArray *arr=@[@{@"progress":@"1",@"image":@"APP切图 订单流程订单跟踪-01",@"title":@"商家已接单",@"content":@"正在联系快递公司",@"time":@"2016-01-22 17:28"},@{@"progress":@"0",@"image":@"切图改-04",@"title":@"寄送物流",@"content":@"没有信息",@"time":@"2016-01-22 17:28"},@{@"progress":@"0",@"image":@"切图改-03",@"title":@"回寄物流",@"content":@"正在联系快递公司",@"time":@"2016-01-22 17:28"},@{@"progress":@"0",@"image":@"切图改-05",@"title":@"检测",@"content":@"还未开始检测",@"time":@"2016-01-22 17:28"},@{@"progress":@"0",@"image":@"切图改-02",@"title":@"分析",@"content":@"还未分析",@"time":@"2016-01-22 17:28"},@{@"progress":@"0",@"image":@"切图改-01",@"title":@"报告",@"content":@"还没出报告",@"time":@"2016-01-22 17:28"},@{@"progress":@"0",@"image":@"切图改-06",@"title":@"分享",@"content":@"还未分享",@"time":@"2016-01-22 17:28"},@{@"progress":@"0",@"image":@"切图改-14",@"title":@"评论",@"content":@"还未评论",@"time":@"2016-01-22 17:28"}];
    
    
    for (int i=0; i<arr.count; i++) {
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(30, 10+150*i, 15, 15)];
        imgView.image=[UIImage imageNamed:arr[i][@"image"]];
        [subView addSubview:imgView];
        if (i>0) {
            imgView.frame=CGRectMake(30-15/2.0, 10+80*i-15, 30, 30);
        }
//        if (i==1) {
//            imgView.frame=CGRectMake(20-15/2.0, 10+80*i-15, 30, 30);
//        }
//        if (i==2) {
//            imgView.frame=CGRectMake(20-15/2.0, 10+80*i-15, 30, 30);
//        }
        
        
        
        UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(35+40, imgView.frame.origin.y, self.view.frame.size.width-(45+20+20), 20)];
        title.text=arr[i][@"title"];
        title.font=[UIFont systemFontOfSize:16];
        [subView addSubview:title];
        if (i>0) {
            title.frame=CGRectMake(35+40, imgView.frame.origin.y+7, self.view.frame.size.width-(45+20+20), 20);
        }
        
        
        
        UILabel *content=[[UILabel alloc]initWithFrame:CGRectMake(title.frame.origin.x, CGRectGetMaxY(title.frame)+6, title.frame.size.width, 12)];
        content.text=arr[i][@"content"];
        content.font=[UIFont systemFontOfSize:12];
        [subView addSubview:content];
        if (i>0) {
            content.frame=CGRectMake(title.frame.origin.x, CGRectGetMaxY(title.frame)+15, title.frame.size.width, 12);
        }
        
        UILabel *time=[[UILabel alloc]initWithFrame:CGRectMake(title.frame.origin.x, CGRectGetMaxY(content.frame)+3, title.frame.size.width, 12)];
        time.text=arr[i][@"time"];
        time.font=[UIFont systemFontOfSize:12];
        [subView addSubview:time];
        
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(time.frame.origin.x, CGRectGetMaxY(time.frame)+5, time.frame.size.width-20, 1)];
        [subView addSubview:line];
        
        UIView *line1=[[UIView alloc]initWithFrame:CGRectMake(30+15/2.0-1, CGRectGetMaxY(imgView.frame)+3, 1, 35)];
        [subView addSubview:line1];
        
        
        UIImageView *imgView1=[[UIImageView alloc]initWithFrame:CGRectMake(line1.frame.origin.x-12/2.0, CGRectGetMaxY(line1.frame)-2, 12,12)];
        [subView addSubview:imgView1];
        
        
        
        
        NSInteger progress=[[NSString stringWithFormat:@"%@",arr[i][@"progress"]] integerValue];
        if (progress==0) {
            title.textColor=[UIColor lightGrayColor];
            content.textColor=[UIColor lightGrayColor];
            time.textColor=[UIColor lightGrayColor];
            line.backgroundColor=[UIColor lightGrayColor];
            line1.backgroundColor=[UIColor lightGrayColor];
            imgView1.image=[UIImage imageNamed:@"APP切图 订单流程订单跟踪-05"];
        }else{
            title.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
            content.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
            time.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
            line.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
            line1.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
            imgView1.image=[UIImage imageNamed:@"APP切图 订单流程订单跟踪-06"];
            
            
            
            //查看详情  寄送物流  回寄物流 分享 评论
            if (i==1||i==2||i==6||i==7) {
                UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame=CGRectMake(self.view.frame.size.width-45, title.frame.origin.y+((CGRectGetMaxY(time.frame)-title.frame.origin.y-25))/2.0, 25, 25);
                [btn setBackgroundImage:[UIImage imageNamed:@"app_right"] forState:UIControlStateNormal];
                btn.tag=i;
                [btn addTarget:self action:@selector(detailBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                [subView addSubview:btn];
                NSInteger reportState=[_dataDic[@"reportStatus"][@"status"] integerValue];
                NSInteger shareState=[_dataDic[@"reportShareStatus"][@"status"] integerValue];
                NSInteger commentState=[_dataDic[@"commentStatus"][@"status"] integerValue];
                if (reportState==2) {
                    if (shareState==1) {
                        btn.hidden=YES;
                    }
                    if (commentState==1) {
                        btn.hidden=YES;
                    }
                }
            }
            
        }
        
    }
    scroll.contentSize=CGSizeMake(scroll.frame.size.width, 10+80*arr.count+20+200+self.view.frame.size.height);
}

//查看详情
-(void)detailBtnClick:(UIButton *)sender
{
    if (sender.tag==1) {
        //寄送物流
         NSInteger state=[[NSString stringWithFormat:@"%@",_dataDic[@"sendInfo"][@"status"]] integerValue];
        if (state==1) {
            //查看物流信息
            LogisticsDetailViewController *vc=[LogisticsDetailViewController new];
            vc.logisticsName=[NSString stringWithFormat:@"%@",_dataDic[@"sendInfo"][@"express_company"]];
            vc.logisticsNumber=[NSString stringWithFormat:@"%@",_dataDic[@"sendInfo"][@"express_number"]];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (state==2){
            //确认收货
            [self gotSampler];
        }


    }else if (sender.tag==2){
        //回寄物流
        NSInteger state=[[NSString stringWithFormat:@"%@",_dataDic[@"receiveInfo"][@"status"]] integerValue];
        if (state==1) {
            //填写物流单号和公司名
        }else if (state==2){
            //查看物流信息
            LogisticsDetailViewController *vc=[LogisticsDetailViewController new];
            vc.logisticsName=[NSString stringWithFormat:@"%@",_dataDic[@"receiveInfo"][@"express_company"]];
            vc.logisticsNumber=[NSString stringWithFormat:@"%@",_dataDic[@"receiveInfo"][@"express_number"]];
            [self.navigationController pushViewController:vc animated:YES];
        }

    }else if (sender.tag==6){
        //分享
        [self shareReport];
        
    }else if (sender.tag==7){
        //评论
        HealthReportCommentViewController *vc=[HealthReportCommentViewController new];
        vc.tradeId=_tradeId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//分享报告
-(void)shareReport
{
    [self.view showTiShiMessage:@"此功能暂未开通"];
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:@"507fcab25270157b37000010"
//                                      shareText:nil
//                                     shareImage:nil
//                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina,UMShareToTencent,UMShareToQzone,UMShareToQQ,UMShareToRenren,UMShareToDouban,nil]
//                                       delegate:self];
}
//-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
//{
//    //根据`responseCode`得到发送结果,如果分享成功
//    if(response.responseCode == UMSResponseCodeSuccess)
//    {
//        //得到分享到的微博平台名
//        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
//        [self.view showTiShiMessage:@"分享成功"];
//    }
//}

//确认收到采样器
-(void)gotSampler
{
    //http://192.168.11.129/api/order/confirm
    //order_id
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token= [user objectForKey:@"token"];
    
    [self.view isLoading:YES];
    
    [manager POST:[NSString stringWithFormat:GotSampler,token] parameters:@{@"order_id":_tradeId} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"确认收到采样器====%@",dic);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self getTradeStateData];
        });
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
        [self.view isLoading:NO];
        [self.view showTiShiMessage:@"程序GG开了个小差"];
    }];
    
}





-(void)createNavigation
{
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"订单追踪";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}

#pragma mark 返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.tabBarController.tabBar.hidden=NO;
    self.navigationController.navigationBar.hidden=NO;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.hidden=NO;
    [self.navigationController.navigationBar setTranslucent:YES];
    [self getTradeStateData];
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
    self.navigationController.navigationBar.hidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getTradeStateData
{
     _dataDic=[NSDictionary dictionary];
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token= [user objectForKey:@"token"];
    
    [self.view isLoading:YES];
    [manager GET:[NSString stringWithFormat:GetTradeState,token,_tradeId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view isLoading:NO];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        _dataDic=[NSDictionary dictionaryWithDictionary:dic];
        _dataArray=[NSMutableArray array];
        NSDictionary *dic1=@{@"progress":@"1",@"image":@"APP切图 订单流程订单跟踪-01",@"title":@"公司已接单",@"content":@"正在寄送采集器",@"time":@"2016-05-22 17:28"};
        [_dataArray addObject:dic1];
        
        
        
        
        //寄出物流
        NSInteger state1=[dic[@"sendInfo"][@"status"] integerValue];
        NSDictionary *dic2;
        if (state1==0) {
            dic2=@{@"progress":dic[@"sendInfo"][@"status"],@"image":@"切图改-04",@"title":@"寄送物流",@"content":@"亲,还没发货哟",@"time":[self getCurrentTime]};
        }else if (state1==1){
            dic2=@{@"progress":dic[@"sendInfo"][@"status"],@"image":@"切图改-10",@"title":@"寄送物流",@"content":[NSString stringWithFormat:@"已发货 物流公司:%@  电话:%@",dic[@"sendInfo"][@"express_company"],dic[@"sendInfo"][@"contact_number"]],@"time":[self getCurrentTime]};
        }else if (state1==2){
            dic2=@{@"progress":dic[@"sendInfo"][@"status"],@"image":@"切图改-10",@"title":@"寄送物流",@"content":@"请确认收货",@"time":[self getCurrentTime]};
        }
        [_dataArray addObject:dic2];
        
        
        //回寄物流
        NSInteger state2=[dic[@"receiveInfo"][@"status"] integerValue];
        NSDictionary *dic3;
        if (state2==0) {
            dic3=@{@"progress":dic[@"receiveInfo"][@"status"],@"image":@"切图改-03",@"title":@"回寄物流",@"content":@"亲,还没发货哟",@"time":@"0000-00-00 00:00"};
        }else if (state2==1){
            dic3=@{@"progress":dic[@"receiveInfo"][@"status"],@"image":@"切图改-09",@"title":@"回寄物流",@"content":@"请填写物流信息",@"time":dic[@"receiveInfo"][@"receive_time"]};
        }else if (state2==2){
            dic3=@{@"progress":dic[@"receiveInfo"][@"status"],@"image":@"切图改-09",@"title":@"回寄物流",@"content":[NSString stringWithFormat:@"已发货 物流公司:%@  电话:%@",dic[@"sendInfo"][@"express_company"],dic[@"sendInfo"][@"contact_number"]],@"time":dic[@"receiveInfo"][@"receive_time"]};
        }
        [_dataArray addObject:dic3];
        
        //检测
        NSInteger state3=[dic[@"experimentStatus"][@"status"] integerValue];
        NSDictionary *dic4;
        if (state3==0) {
            dic4=@{@"progress":dic[@"experimentStatus"][@"status"],@"image":@"切图改-05",@"title":@"检测",@"content":@"还未检测",@"time":@"0000-00-00 00:00"};
        }else if (state3==1){
            dic4=@{@"progress":dic[@"experimentStatus"][@"status"],@"image":@"切图改-11",@"title":@"检测",@"content":@"样品抽取",@"time":dic[@"experimentStatus"][@"start_time"]};
        }else if (state3==2){
            dic4=@{@"progress":dic[@"experimentStatus"][@"status"],@"image":@"切图改-11",@"title":@"检测",@"content":@"样品检测合格",@"time":dic[@"experimentStatus"][@"finish_time"]};
        }
        [_dataArray addObject:dic4];
        
        
        //分析
        NSInteger state4=[dic[@"analyseStatus"][@"status"] integerValue];
        NSDictionary *dic5;
        if (state4==0) {
            dic5=@{@"progress":dic[@"analyseStatus"][@"status"],@"image":@"切图改-02",@"title":@"分析",@"content":@"还未分析",@"time":@"0000-00-00 00:00"};
        }else if (state4==1){
            dic5=@{@"progress":dic[@"analyseStatus"][@"status"],@"image":@"切图改-08",@"title":@"分析",@"content":@"正在分析",@"time":dic[@"analyseStatus"][@"start_time"]};
        }else if (state4==2){
            dic5=@{@"progress":dic[@"analyseStatus"][@"status"],@"image":@"切图改-08",@"title":@"分析",@"content":@"分析完成",@"time":dic[@"analyseStatus"][@"finish_time"]};
        }
        [_dataArray addObject:dic5];
        
        //报告
        NSInteger state5=[dic[@"reportStatus"][@"status"] integerValue];
        NSDictionary *dic6;
        if (state5==0) {
            dic6=@{@"progress":dic[@"reportStatus"][@"status"],@"image":@"切图改-01",@"title":@"报告",@"content":@"还未出报告",@"time":@"0000-00-00 00:00"};
        }else if (state5==1){
            dic6=@{@"progress":dic[@"reportStatus"][@"status"],@"image":@"切图改-07",@"title":@"报告",@"content":@"正在出报告",@"time":dic[@"reportStatus"][@"start_time"]};
        }else if (state5==2){
            dic6=@{@"progress":dic[@"reportStatus"][@"status"],@"image":@"切图改-07",@"title":@"报告",@"content":@"已经出报告",@"time":dic[@"reportStatus"][@"finish_time"]};
        }
        [_dataArray addObject:dic6];
        
        //分享
        NSInteger state6=[dic[@"reportShareStatus"][@"status"] integerValue];
        NSDictionary *dic7;
        if (state6==0) {
            dic7=@{@"progress":dic[@"reportShareStatus"][@"status"],@"image":@"切图改-06",@"title":@"分享",@"content":@"还未分享",@"time":@"0000-00-00 00:00"};
        }else if (state6==1){
            dic7=@{@"progress":dic[@"reportShareStatus"][@"status"],@"image":@"切图改-12",@"title":@"分享",@"content":@"已分享",@"time":dic[@"reportShareStatus"][@"finish_time"]};
        }
        [_dataArray addObject:dic7];
        
        //评论
        NSInteger state7=[dic[@"commentStatus"][@"status"] integerValue];
        NSDictionary *dic8;
        if (state7==0) {
            dic8=@{@"progress":dic[@"commentStatus"][@"status"],@"image":@"切图改-14",@"title":@"评论",@"content":@"暂时不能评论",@"time":@"0000-00-00 00:00"};
        }else if (state7==1){
            dic8=@{@"progress":dic[@"commentStatus"][@"status"],@"image":@"切图改-13",@"title":@"评论",@"content":@"已评论",@"time":dic[@"commentStatus"][@"finish_time"]};
        }
        [_dataArray addObject:dic8];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self createUI];
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view isLoading:NO];
        [self.view showTiShiMessage:@"程序员GG开了个小差"];
        NSLog(@"订单状态error%@",error.localizedDescription);
    }];
}

-(NSString *)getCurrentTime
{
    NSString *time;
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    time=[formatter stringFromDate:[NSDate date]];
    return time;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
